package com.darly.book.presenter;

import com.darly.book.model.bean.packages.BillboardPackage;
import com.darly.book.model.remote.BookApi;
import com.darly.book.presenter.contract.BillboardContract;
import com.darly.book.ui.base.RxPresenter;
import com.darly.book.utils.Constant;
import com.darly.dlcommon.retrofit.RxjavaRetrofitRequestUtil;

import io.reactivex.SingleObserver;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class BillboardPresenter extends RxPresenter<BillboardContract.View>
        implements BillboardContract.Presenter {

    @Override
    public void loadBillboardList() {
        //这个最好是设定一个默认时间采用Remote加载，如果Remote加载失败则采用数据中的数据。我这里先写死吧
//        BillboardPackage bean = LocalRepository.getInstance()
//                .getBillboardPackage();
//        if (bean == null) {
        RxjavaRetrofitRequestUtil.getInstance().getWithHost(BookApi.class, Constant.API_BASE_URL)
                .getBillboardPackage()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSuccess(
                        (value) -> {
                            Schedulers.io().createWorker()
                                    .schedule(
                                            () -> {
//                                                    LocalRepository.getInstance()
//                                                            .saveBillboardPackage(value);
                                            }
                                    );
                        }
                )
                .subscribe(new SingleObserver<BillboardPackage>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onSuccess(BillboardPackage value) {
                        mView.finishRefresh(value);
                        mView.complete();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError();
                    }
                });
//        } else {
//            mView.finishRefresh(bean);
//            mView.complete();
//        }
    }
}
